package hfxy.dzx.tx.main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import hfxy.dzx.tx.model.Circle;

public class TestSet {
	public static void main(String []args) {
		Set<Circle> set=new HashSet();
		
		//增加数据
		set.add(new Circle(1,1,3));
		set.add(new Circle(1,1,1));
		set.add(new Circle(1,1,2));
		
		//遍历
		System.out.println("直接打印set"+set);
		
		//1，不可以
		for(int i=0;i<set.size();i++) {
			//System.out.println(set.get(i));错误
		}
		//2，从结果上看是无序的
		for(Circle c:set) {
			System.out.println(c);
		}
		
		//3
		Iterator<Circle> iterator=set.iterator();
		while(iterator.hasNext()) {
			Circle c=(Circle) iterator.next();			
			System.out.println(c);
		}
		
		//排序,hashset排序借助list
		ArrayList<Circle> list=new ArrayList<>(set);
		
		Collections.sort(list);//必须定义Comparable内比较器，与自己比，故一个参数
		System.out.println("内比较器"+list);
		Collections.sort(list,new OutComp2());
		System.out.println("外比较器"+list);
		
		//排序,TreeSet排序，可以直接排序
		TreeSet<Circle> treeset=new TreeSet<>();
		treeset.add(new Circle(1,1,33));
		treeset.add(new Circle(1,1,11));
		treeset.add(new Circle(1,1,22));
		System.out.println("treeset"+treeset);
	}

}

class OutComp2 implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		return (int)( ((Circle)o1).getR()-((Circle)o2).getR() );
		//return 0;
	}
	
}
